<template>
  <div class="addRole">
    <el-dialog
      :title="isEdit ? '编辑角色' : '新建角色'"
      :visible.sync="dialogVisible"
      width="40%"
      :before-close="dialogClose">
      <el-form ref="form" :model="form" label-width="140px"  size="mini">
        <el-form-item label="角色名称" style="width:300px" prop="name">
          <el-input v-model="form.name"></el-input>
        </el-form-item>
         <el-form-item label="角色编码" style="width:300px" prop="code">
          <el-input v-model="form.code"></el-input>
        </el-form-item>
         <el-form-item label="描述" style="width:300px" prop="description">
          <el-input v-model="form.description" type="textarea"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogClose" size="small">取 消</el-button>
        <el-button type="primary" @click="onSave" size="small">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { roleSaveOrUpdate } from '@/services/role'
export default {
  name: 'addRole',
  props: {
    isEdit: {
      type: Boolean
    }
  },
  data () {
    return {
      dialogVisible: false,
      form: {}
    }
  },
  methods: {
    // 关闭弹窗
    dialogClose () {
      this.dialogVisible = false
      this.form = {}
    },
    // 开启弹窗
    dialogOpen () {
      this.dialogVisible = true
    },
    dialogEdit (val) {
      this.dialogVisible = true
      this.form = val
    },
    // 保存数据
    async onSave () {
      const { data } = await roleSaveOrUpdate(this.form)
      if (data.code === '000000') {
        this.$message.success('保存成功')
        // 关闭弹窗
        this.dialogClose()
        // 刷新列表页
        this.$emit('refrshRole')
      }
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
